package com.newjiabo.ski.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.newjiabo.common.ski.media.module.enums.MediaStatusEnum;
import com.newjiabo.common.ski.media.utils.MediaRequestService;
import com.newjiabo.common.utils.SecurityUtils;
import com.newjiabo.common.utils.SnowFlake;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.ski.domain.SkiMemberBodyInfo;
import com.newjiabo.ski.mapper.SkiMemberBodyInfoMapper;
import com.newjiabo.ski.service.ISkiMemberBodyInfoService;
import com.newjiabo.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

/**
 * 会员身份信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-17
 */
@Service
public class SkiMemberBodyInfoServiceImpl implements ISkiMemberBodyInfoService {
    @Resource
    private SkiMemberBodyInfoMapper skiMemberBodyInfoMapper;
    @Resource
    private MediaRequestService mediaRequestService;

    /**
     * 查询会员身份信息
     *
     * @param id 会员身份信息主键
     * @return 会员身份信息
     */
    @Override
    public SkiMemberBodyInfo selectSkiMemberBodyInfoById(Long id) {
        return skiMemberBodyInfoMapper.selectSkiMemberBodyInfoById(id);
    }

    /**
     * 查询会员身份信息列表
     *
     * @param skiMemberBodyInfo 会员身份信息
     * @return 会员身份信息
     */
    @Override
    public List<SkiMemberBodyInfo> selectSkiMemberBodyInfoList(SkiMemberBodyInfo skiMemberBodyInfo) {
        return skiMemberBodyInfoMapper.selectSkiMemberBodyInfoList(skiMemberBodyInfo);
    }

    /**
     * 新增会员身份信息
     *
     * @param skiMemberBodyInfo 会员身份信息
     * @return 结果
     */
    @Override
    public int insertSkiMemberBodyInfo(SkiMemberBodyInfo skiMemberBodyInfo) {
        skiMemberBodyInfo.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isEmpty(skiMemberBodyInfo.getId())) {
            skiMemberBodyInfo.setId(SnowFlake.getIdStr());
        }
        return skiMemberBodyInfoMapper.insertSkiMemberBodyInfo(skiMemberBodyInfo);
    }

    /**
     * 修改会员身份信息
     *
     * @param skiMemberBodyInfo 会员身份信息
     * @return 结果
     */
    @Override
    public int updateSkiMemberBodyInfo(SkiMemberBodyInfo skiMemberBodyInfo) {
        skiMemberBodyInfo.setUpdateTime(DateUtils.getNowDate());
        return skiMemberBodyInfoMapper.updateSkiMemberBodyInfo(skiMemberBodyInfo);
    }

    /**
     * 批量删除会员身份信息
     *
     * @param ids 需要删除的会员身份信息主键
     * @return 结果
     */
    @Override
    public int deleteSkiMemberBodyInfoByIds(Long[] ids) {
        return skiMemberBodyInfoMapper.deleteSkiMemberBodyInfoByIds(ids);
    }

    /**
     * 删除会员身份信息信息
     *
     * @param id 会员身份信息主键
     * @return 结果
     */
    @Override
    public int deleteSkiMemberBodyInfoById(Long id) {
        return skiMemberBodyInfoMapper.deleteSkiMemberBodyInfoById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String registerImageInfo(SkiMemberBodyInfo skiMemberBodyInfo) {
        this.initMemberBodyInfo(skiMemberBodyInfo);
        this.insertSkiMemberBodyInfo(skiMemberBodyInfo);
        String mediaIds = StringUtils.join(skiMemberBodyInfo.getFullBodyPhotoMediaId(),skiMemberBodyInfo.getFrontalViewMediaId());
        String userId = skiMemberBodyInfo.getMemberId();
        JSONObject jsonObject = mediaRequestService.registerPerson(userId, mediaIds, null, null);
        return jsonObject.toString();
    }

    @Override
    public SkiMemberBodyInfo getOneRecentBySnowFieldId(String snowFieldId) {
        return skiMemberBodyInfoMapper.getOneRecentBySnowFieldId(snowFieldId, SecurityUtils.getMemberId());
    }

    private void initMemberBodyInfo(SkiMemberBodyInfo info){
        info.setRegisterStatus(MediaStatusEnum.Processing.name());
    }
}
